<template>
  <!-- 放置一个图标 -->
  <div>
    <svg-icon
      :icon-class="isFull ? 'exit-fullscreen' : 'fullscreen'"
      class="fullscreen"
      @click="togleScreen"
    />
  </div>
</template>

<script>
import sFull from 'screenfull'
export default {
  name: 'ScreenFull',
  data () {
    return {
      isFull: false
    }
  },
  created () {
    this.onScreen()
  },
  beforeDestroy () {
    // 关闭监控
    sFull.off('change')
  },
  methods: {
    // 监控浏览器是否是全屏状态
    // 语法:sFull.on('change',callback)
    onScreen () {
      sFull.on('change', () => {
        console.log(sFull.isFullscreen)
        if (sFull.isFullscreen) {
          this.isFull = true
        } else {
          this.isFull = false
        }
      })
    },
    // 开启或关闭全屏
    togleScreen () {
      /**
       * 选择screenFull 插件 1.浏览器兼容性判断 2.提供了好用简洁的方法
       * 1.判断兼容性 => sfull.isEnabled 返回true说明当前浏览器支持,相反不支持
       * 2.点击执行调用sereenfull暴利提供的方法
       */
      if (!sFull.isEnabled) {
        //  不支持
        return this.$message.error('浏览器支持')
      }
      // 支持
      // sFull.request()
      sFull.toggle()
    }
  }
}
</script>

<style lang="scss" scoped>
.fullscreen {
  width: 20px;
  height: 20px;
  color: #fff;
  cursor: pointer;
}
</style>
